<template>
    <div>
        <el-card class="box-card">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>智慧书店</el-breadcrumb-item>
                <el-breadcrumb-item>书籍类型列表</el-breadcrumb-item>
            </el-breadcrumb>

            <div>
            <el-button @click="dialogAdd = true">添加类型</el-button>
            </div>
            <el-table :data="bookTypeList" border style="width: 800px;height: 100%">
                <el-table-column prop="id" label="书籍类型编号"></el-table-column>
                <el-table-column prop="bookTypeName" label="书籍类型名称"></el-table-column>
                <el-table-column fixed="right" label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button @click="toUpdate(scope.row.id)" type="text" size="small">重命名</el-button>
                        <el-button @click="delType(scope.row.id)" type="text" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>


            <el-dialog title="添加类型" :visible.sync="dialogAdd">
                <el-form :model="type">
                    <el-form-item label="书籍类型名称" :label-width="formLabelWidth" label-width="120px">
                        <el-input v-model="type.bookTypeName" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogAdd = false">取 消</el-button>
                    <el-button type="primary" @click="addType">确 定</el-button>
                </div>
            </el-dialog>
            <el-dialog title="重命名" :visible.sync="dialogFormVisible">
                <el-form :model="type">
                    <el-form-item label="书籍类型名称" :label-width="formLabelWidth" label-width="120px">
                        <el-input v-model="type.bookTypeName" autocomplete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="updateType">确 定</el-button>
                </div>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "List",
        data(){
            return{
                bookTypeList:[],
                dialogFormVisible:false,
                formLabelWidth: '80px',
                dialogAdd:false,
                type:{
                    bookTypeName:''
                },
            }
        },methods:{
            findAll(){
                this.$http.post('/book/findAllBookType').then(res =>{
                      if (res.data.success){
                          this.bookTypeList = res.data.result.bookTypeList;
                      } else {
                          this.$message.error(res.data.message);
                      }
                  })
            },updateType(){
                this.$http.post('/book/updateBookType',this.type).then(res =>{
                    if (res.data.success){
                        this.findAll()
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
                this.type = {};
                this.dialogFormVisible = false;
            },addType(){
                this.$http.post('/book/addBookType',this.type).then(res =>{
                    if (res.data.success){
                        this.findAll()
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
                this.type = {};
                this.dialogAdd = false;
            },delType(id){
                this.$confirm('确认删除吗？','提示').then(()=>{
                    this.$http.post('/book/delBookType?bookTypeId='+id).then(res =>{
                          if (res.data.success){
                              this.findAll()
                          } else {
                              this.$message.error(res.data.message);
                          }
                      })
                }).catch(()=>{})
            },toUpdate(id){
                this.type.id = id;
                this.dialogFormVisible = true;
            }
        },mounted(){
            this.findAll()
        }
    }
</script>

<style scoped>

</style>
